package com.IntelligenceInsight.Myth.service;


import com.IntelligenceInsight.Myth.mapper.MLJobMapper;
import com.IntelligenceInsight.Myth.model.MLJob;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MLJobService {
    @Autowired
    private MLJobMapper mapper;

    public NoteRequest MLJobListAll(int page, int limit) {
        NoteRequest res = new NoteRequest();
        List<MLJob> mljob = mapper.MLJobListAll(page, limit);
        if(mljob == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(mljob);
        }
        return res;
    }

    public int MLJobListAllCount() {
        int count = mapper.MLJobListAllCount();
        return count;
    }
    public NoteRequest MLJobListByOwner(String owner) {
        NoteRequest res = new NoteRequest();
        List<MLJob> mljob = mapper.MLJobListByOwner(owner);
        if(mljob == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(mljob);
        }
        return res;
    }

    public NoteRequest MLJobByUuid(String uuid) {
        NoteRequest res = new NoteRequest();
        MLJob mljob = mapper.MLJobByUuid(uuid);
        if(mljob == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(mljob);
        }
        return res;
    }

    public NoteRequest MLJobByName(String name) {
        NoteRequest res = new NoteRequest();
        MLJob mljob = mapper.MLJobByName(name);
        if(mljob == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(mljob);
        }
        return res;
    }

    public NoteRequest MLJobByAlgorithm(String algorithm) {
        NoteRequest res = new NoteRequest();
        List<MLJob> mljob = mapper.MLJobByAlgorithm(algorithm);
        if(mljob == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(mljob);
        }
        return res;
    }

    public NoteRequest MLJobAdd(String uuid, String name, String algorithm, String owner, String parameters, String resultPath) {
        NoteRequest res = new NoteRequest();
        int n = mapper.MLJobAdd(uuid, name, algorithm, owner, parameters, resultPath);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: " + uuid);
        }
        return res;
    }

    public NoteRequest MLJobStateChange(String uuid, String state, String result) {
        NoteRequest res = new NoteRequest();
        int n = mapper.MLJobStateChange(uuid, state, result);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: " + uuid);
        }
        return res;
    }

    public NoteRequest MLJobDelete(String uuid) {
        NoteRequest res = new NoteRequest();
        int n = mapper.MLJobDelete(uuid);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: " + uuid);
        }
        return res;
    }
}
